package com.roomio.core.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.roomio.core.dao.CodelistDAO;
import com.roomio.core.types.Codelist;

@Repository("codelistDAO")
public class CodelistDAOImpl extends GenericJPADAO<Codelist, Long> implements CodelistDAO {
	protected final Logger log = LoggerFactory.getLogger(getClass());
	private String ORDERBY_CLAUSE = " order by cd.order, cd.name asc";
	final private String WHERE_CLAUSE = " (cd.isActive = true or cd.isActive is null)";

	@Override
	public <T extends Codelist> List<T> findValuesByType(String codeType) {
		StringBuffer query = new StringBuffer("from " +codeType + " cd ");
		query.append("where " + WHERE_CLAUSE);
		query.append(ORDERBY_CLAUSE);
		Query hsql = entityManager.createQuery(query.toString());
		return hsql.getResultList();
		
//		return hql.list();
	}

	@Override
	public <T extends Codelist> List<T> findValuesByTypeAndCriteria(String codeType, String hqlCriteria) {
		StringBuffer query = new StringBuffer("from " + codeType + " cd ");
		query.append("where " + hqlCriteria + " and " + WHERE_CLAUSE);
		query.append(ORDERBY_CLAUSE);
		Query hsql = entityManager.createQuery(query.toString());
		return hsql.getResultList();

//		Query hql = getSession().createQuery(query.toString());
		//		return hql.list();
	}
	
//	public <X extends Codelist> void save(X x) {
//		simplifyEmf.persist(x);
//	}

}